﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using SilverlightApplication1.GdiService;
using System.ComponentModel;

namespace SilverlightApplication1
{
    public class MainPageViewModel: BaseViewModel
    {

        GdiStoryServiceClient _client = new GdiStoryServiceClient();
        public MainPageViewModel()
        {
            _client.SearchGoogleCompleted += new EventHandler<SearchGoogleCompletedEventArgs>(_client_SearchGoogleCompleted);
            _client.SearchGoogleNewsCompleted += new EventHandler<SearchGoogleNewsCompletedEventArgs>(_client_SearchGoogleNewsCompleted);


            //_client.SearchGoogleAsync("Silverlight");
            //_client.SearchGoogleNewsAsync("Lacrosse");

            SetupCommands();

            Pages.Add(new HomeView());
            Pages.Add(new ShopView());
            Pages.Add(new AboutView());
            Pages.Add(new SearchView());
        }

        private void SetupCommands()
        {
            HomeCommand = new MyCommand(GoHome, CanGoHome);
            AboutCommand = new MyCommand(GoAbout, CanGoAbout);
            ShopCommand = new MyCommand(GoShop, null);
            SearchCommand = new MyCommand(GoSearch, null);
        }

        #region Home Command
        public ICommand HomeCommand { get; set; }
        private void GoHome(object o)
        {
            Pages[0] = new HomeView();
            CurrentPage = Pages[0];
        }

        private bool CanGoHome(object o)
        {
            return true;
        }
        #endregion

        #region About Command
        public ICommand AboutCommand { get; set; }
        private void GoAbout(object o)
        {
            CurrentPage = Pages[2];
        }

        private bool CanGoAbout(object o)
        {
            return true;
        }
        #endregion

        #region Shop Command
        public ICommand ShopCommand { get; set; }
        private void GoShop(object o)
        {
            CurrentPage = Pages[1];
        }

        #endregion

        #region Search Command
        public ICommand SearchCommand { get; set; }
        private void GoSearch(object o)
        {
            CurrentPage = Pages[3];
        }
        #endregion

        void _client_SearchGoogleNewsCompleted(object sender, SearchGoogleNewsCompletedEventArgs e)
        {
            NewsResults = e.Result;
        }

        void _client_SearchGoogleCompleted(object sender, SearchGoogleCompletedEventArgs e)
        {

            SearchResults = e.Result;
        }



        private UserControl _currentPage;
        public UserControl CurrentPage
        {
            get
            {
                return _currentPage;
            }
            set
            {
                _currentPage = value;
                RaisePropertyChanged("CurrentPage");
            }
        }

        private ObservableCollection<UserControl> _pages = new ObservableCollection<UserControl>();
        public ObservableCollection<UserControl> Pages
        {
            get
            {
                return _pages;
            }
            set
            {
                _pages = value;
                RaisePropertyChanged("Pages");
            }
        }

        private ObservableCollection<GoogleNewsResult> _newsResults = new ObservableCollection<GoogleNewsResult>();
        public ObservableCollection<GoogleNewsResult> NewsResults
        {
            get
            {
                return _newsResults;
            }
            set
            {
                _newsResults = value;
                RaisePropertyChanged("NewsResults");
            }
        }
        private ObservableCollection<GoogleSearchResult> _searchResults = new ObservableCollection<GoogleSearchResult>();
        public ObservableCollection<GoogleSearchResult> SearchResults
        {
            get
            {
                return _searchResults;
            }
            set
            {
                _searchResults = value;
                RaisePropertyChanged("SearchResults");
            }
        }


    }
}
